/**
 * Created by panjun7@yunzongnet.com on 2016/10/25.
 */
BusinessMonthlyReportController = function () {
    this.configInfo = {
        pieChartTPL:{'option':{
            tooltip: {
                trigger: 'item',
                formatter: "{b}:<br/> {c} ({d}%)"
            },
            legend: {
                orient: 'vertical',
                left: 'right',
                top:'middle',
                selectedMode:false,
                data:['支付宝','现金','会员卡','银联','微信支付']
            },
            toolbox: {
                show:false,
            },
            color:['#7eacf6','#49d0a8','#be9dff','#dfc891','#ffa18c'],
            series: [
                {
                    name:'收款渠道分析',
                    type:'pie',
                    radius: ['50%', '70%'],
                    avoidLabelOverlap: false,
                    hoverAnimation:false,
                    center: ['40%', '50%'],
                    label: {
                        normal: {
                            show: true,
                            position: 'center',
                            silent:true,
                            formatter: function(p) {
                                return "";
                            },
                            textStyle: {
                                color: '#000000',
                                fontWeight: 'bold'
                            }

                        },
                       
                        emphasis: {
                            show: false,
                            textStyle: {
                                fontSize: '30',
                                fontWeight: 'bold'
                            }
                        }
                    },
                    labelLine: {
                        normal: {
                            show: false
                        }
                    },
                    data:[
                        
                    ]
                }
            ]
        }}
    };
    this.initPage = function() {
        var self = this;
        self.initMonthlyReportData();
        self.initChannelAnaData();
        self.initSelectDate();

    };
    this.initSelectDate = function(){
        var future = CommonUtil.getUserServiceInfo();
        var self = this;
        var intervalObj = window.setInterval(function(){
                                                    if(future.finish){
                                                        intervalObj && window.clearInterval(intervalObj);
                                                        if(future.success){
                                                            var currYear = new Date().getFullYear();
                                                            $('#monthly-report-select-date').mobiscroll().date({
                                                                theme: 'ios',
                                                                model: 'scroller',
                                                                lang: 'zh',
                                                                display: 'bottom',
                                                                dateFormat: 'yy-mm',
                                                                yearSuffix:'年',
                                                                monthSuffix:'月',
                                                                onBeforeShow: function (inst) { inst.settings.wheels[0].length>2?inst.settings.wheels[0].pop():null; },
                                                                defaultValue: new Date(new Date().setFullYear(currYear)),
                                                                maxDate: new Date(),
                                                                minDate: new Date(future.data.orderTime.substring(0,10)),
                                                                onClose: function (valueText, inst) {
                                                                   
                                                                },
                                                                onSelect:function(valueText,inst){
                                                                    var params = {"bizDate":valueText.replace("-","")};
                                                                    self.initMonthlyReportData(params);
                                                                    self.initChannelAnaData(params);
                                                                }
                                                           });
                                                        }else{
                                                            //不影响用户其他操作，可以忽略
                                                        }
                                                        
                                                    }
                                                },
                                    100);           
    }

    //初始化经营报表数据
    this.initMonthlyReportData = function(params){
        if(!params){
            params = {"bizDate":DateUtil.getDateYYYYMM(new Date())};
        }

        var serverUrl = App.serverUrl +　"merchantApp/businessReport/queryTradeMonthReportData.do";
        var self = this;
        var sucFunc = function (result) {
            // 渲染模板
            for(var key in result.resultObject){
                result.resultObject[key] = CommonUtil.nullValueParser(result.resultObject,key,'--');
            }

            var bizDate = params.bizDate;

            var year = bizDate.substring(0,4);
            var month = bizDate.substring(4);
            if(month<10){
                month = month.substring(1);
            }

            var reportTitle = year+"年"+month+"月累计";
            result.resultObject.reportTitle = reportTitle;

            CommonUtil.render($("#monthlyReportPage .today-data"),"monthlyReportDataTPL",result.resultObject);
        };

        var failFunc = function () {
            log4JS.info("queryTradeMonthReportData failed. http status not 200");
            DialogUtil.dialogWarn();
        };
        CommonUtil.sendJsonpRequest({"url":serverUrl,"success":sucFunc,"error":failFunc,"params":params});
    };

    //初始化收款渠道分析数据
    this.initChannelAnaData = function(params){
        if(!params){
            params = {"bizDate":DateUtil.getDateYYYYMM(new Date())};
        }
        var serverUrl = App.serverUrl +　"merchantApp/businessReport/queryTradeChannelAnaData.do";
        var self = this;
        var sucFunc = function (result) {
            var trendReportChart = echarts.init(document.getElementById("channelAnaChart"),'normal');
            var option = self.configInfo.pieChartTPL.option;
            trendReportChart.setOption(option);

            var data = new Array();
            data.push({"name":"支付宝",
                       "value":CommonUtil.nullValueParser(result.resultObject,'aliPayAmount','0')
                       });

            data.push({"name":"现金",
                      "value":CommonUtil.nullValueParser(result.resultObject,'cashAmount','0')});

            data.push({"name":"会员卡",
                        "value":CommonUtil.nullValueParser(result.resultObject,'memberCardAmount','0')});

            data.push({"name":"银联",
                       "value":CommonUtil.nullValueParser(result.resultObject,'unionpayAmount','0')});
            
            data.push({"name":"微信支付",
                      "value":CommonUtil.nullValueParser(result.resultObject,'weixinAmount','0')});
           
            
            
            
            option.series[0].data = data;
            option.series[0].label.normal.formatter = function(){
                 return "实收金额（元）\n" + CommonUtil.nullValueParser(result.resultObject,'totalAmount','0');

            };
            trendReportChart.setOption(option);

        };

        var failFunc = function () {
            log4JS.info("queryTradeMonthReportData failed. http status not 200");
            DialogUtil.dialogWarn();
        };
        CommonUtil.sendJsonpRequest({"url":serverUrl,"success":sucFunc,"error":failFunc,"params":params});
    };

    
};